<template>
  <el-container>
    <el-header style="height:80px;">
      <el-form
        ref="form"
        label-width="80px"
        style="margin-top:20px;"
        :model="search"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="部门名称：">
              <el-input v-model="search.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="部门类型">
              <el-select v-model="search.type">
                <el-option
                  label="全部"
                  value=""
                ></el-option>
                <el-option
                  v-for="(item,index) in departType"
                  :key="index"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="上级部门">
              <el-input v-model="search.fathername"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-button
              class="querybtn"
              type="primary"
              @click="findDepart(limit,1)"
              style="color: #606266;background-color: #fff;border-color: #dcdfe6;margin-left:10px;"
            >查询</el-button>
            <el-button
              type="primary"
              @click="handleAdd"
            >新增</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-header>
    <el-dialog
      :title="title"
      :visible.sync="dialogFormVisible"
      width="30%"
      @closed="closedDialog"
    >
      <el-form
        label-width="100px"
        ref="departDialog"
        :model="addDepart"
        class="demo-ruleForm"
      >
        <el-row>
          <el-form-item
            label="部门名称："
            prop="name"
            :rules="[{required: true, message: '部门名称不能为空', trigger: 'blur'}]"
          >
            <el-input
              v-model="addDepart.name"
              placeholder="部门名称"
              auto-complete="off"
              maxlength="30"
              style="width: 185px"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item
            label="部门类型"
            prop="type"
            :rules="[{required: true, message: '部门类型不能为空', trigger: 'blur'}]"
          >
            <el-select
              @change="onDepartTypeChange"
              v-model="addDepart.type"
            >
              <el-option
                v-for="(item,index) in departType"
                :key="index"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row v-if="addDepart.type===2">
          <el-form-item label="业务口">
            <el-input
              v-model="addDepart.serviceport"
              style="width: 185px;"
            ></el-input>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="上级部门">
            <el-select
              v-model="addDepart.fatherid"
              :disabled="addDepart.type===1"
            >
              <el-option
                label="(无)"
                style="opacity: 0.6"
                :value="0"
              ></el-option>
              <el-option
                v-for="(item,index) in superiorDeparts"
                :key="index"
                :value="item.id"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
        style="text-align: center"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="danger"
          @click="save()"
        >保 存</el-button>
      </div>
    </el-dialog>
    <el-main style="height:800px;">
      <section class="contant_head">
        <span>图表
          <!-- <sub>Form</sub> -->
        </span>
      </section>
      <el-table
        :data="tableData"
        border
        style="width: 98%;margin:10px;text-align:center;"
        :header-cell-style="getRowClass"
      >
        <el-table-column
          label="编号"
          width="90"
          type="index"
        > </el-table-column>
        <el-table-column
          prop="name"
          label="部门名称"
        ></el-table-column>
        <el-table-column
          prop="createtime"
          label="创建时间"
          :formatter="parseTime"
        ></el-table-column>
        <el-table-column
          prop="type"
          label="类型"
          :formatter="typeFormat"
        ></el-table-column>
        <el-table-column
          prop="serviceport"
          label="业务口"
        ></el-table-column>
        <el-table-column
          prop="fathername"
          label="上级部门"
        ></el-table-column>
        <el-table-column
          label="操作"
          width="150"
        >
          <template slot-scope="scope">

            <el-button
              class="edit-btn"
              @click="handleEdit(scope.$index, scope.row)"
              size="medium"
            >
              修改
            </el-button>
            <el-button
              class="edit-btn"
              v-if="scope.row.state===1"
              size="medium"
              @click="toggleStatus(scope.row.id,2)"
              type="danger"
            >
              删除
            </el-button>
            <!-- <el-button
              v-else
              class="edit-btn"
              size="medium"
              @click="toggleStatus(scope.row.id,1)"
            >
              启用
            </el-button> -->
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          layout="total, prev, pager, next"
          :page-size="limit"
          :total="total"
          style="float:right"
        >
        </el-pagination>
      </div>
    </el-main>
  </el-container>
</template>

<script>
import { api } from "@/fetch/api";


export default {
  name: "joggle",
  data() {
    return {
      options: [
        {
          value: 1,
          label: "全选"
        },
        {
          value: 2,
          label: "启用"
        },
        {
          value: 3,
          label: "禁用"
        }
      ],
      search: {
        name: '',
        type: '',
        fatherid: '',
        fathername: ''
      },
      superiorDeparts: [],
      departType: [
        {
          value: 1,
          label: '支队'
        },
        {
          value: 2,
          label: '科室'
        },
        {
          value: 3,
          label: '大队'
        },
        {
          value: 4,
          label: '中队'
        }
      ],
      permissions: [],
      tableData: [],
      dialogVisible: false,
      dialogTableVisible: false,
      dialogFormVisible: false,
      currentPage: 1, //當前頁碼
      limit: 10,
      total: null,
      edit: false,
      title: "",
      addDepart: {
        name: '',
        fatherid: '',
        type: ''
      }
    };
  },
  methods: {
    handleAdd() {
      // 新增角色
      this.dialogFormVisible = !this.dialogFormVisible;
      this.$refs['departDialog'] && this.$refs['departDialog'].clearValidate();
      this.title = "添加";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    gitButtonId(index) {
      return index;
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.findDepart(this.limit, val);
    },
    handleSizeChange(val) {
      this.limit = val;
      this.findDepart(val, this.currentPage);
    },
    handleDelete(index, row) {
      this.$confirm("是否继续删除", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api
            .Depattmentdelete({
              departmentId: row.cDepartmentId
            })
            .then(response => {
              if (response.status === 100) {
                this.$message.success(response.message);
                this.getUser(this.limit, this.currentPage);
              } else {
              }
            });
        })
        .catch(() => { });
    },
    handleEdit(index, row) {
      this.dialogFormVisible = !this.dialogFormVisible;
      this.title = "修改";
      this.findDepartmentById(row.id)
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    curry(e, node) {
      if (e.target.checked) {
        node.subDetail.forEach((sub, i) => {
          node.subDetail[i].checked = true;
        });
      } else {
        node.subDetail.forEach((sub, i) => {
          node.subDetail[i].checked = false;
        });
      }
    },
    getPermissionDetail() {
      api
        .PermissionPage({}, { limit: 0, pageIndex: 0 })
        .then(response => {
          if (response.status === 100) {
            this.permissions = response.result;
            this.myrole.rolepermissionlist = response.result;
            api.LocalStorageService.update("permissions", response.result);
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    },
    getRowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex == 0) {
        return "background:rgba(242,242,242,1)";
      } else {
        return "";
      }
    },
    save() {
      this.$refs['departDialog'].validate(valid => {
        if (valid) {
          if (this.title === '添加') {
            this.addDepartment()
          }
          if (this.title === '修改') {
            this.updateDepartment()
          }
        } else {
          this.$message({
            message: '请完善表单'
          });
          return false;
        }
      })
    },
    findDepart(limit, currentPage) {
      if (currentPage === 1) {
        this.currentPage = 1
      }
      api.findDepartment(limit, currentPage, this.search).then(res => {
        this.tableData = res.result.inspectorList
        this.total = res.result.totalCount
      })
    },
    getSuperiorDepart(id) {
      if (id === 2 || id === 3) {
        id = 1
      } else if (id === 4) {
        id = 3
      }
      api.findDepartment(0, 0, { type: id }).then(res => {
        this.superiorDeparts = res.result.inspectorList
      })
    },
    onDepartTypeChange(val) {
      if (val !== 1) {
        this.addDepart.fatherid = ''
        this.getSuperiorDepart(val)
      } else {
        this.addDepart.fatherid = 0
      }
    },
    addDepartment() {
      api.addDepartment(this.addDepart).then(res => {
        if (res.status === 100) {
          this.dialogFormVisible = false
          this.findDepart(this.limit, this.currentPage)
          this.$message({
            type: 'success',
            message: "部门添加成功"
          })
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    findDepartmentById(id) {
      api.findDepartmentById(id).then(res => {
        const { name, fatherid, type, id } = res.result
        this.addDepart = {
          id,
          name,
          fatherid,
          type
        }

        this.getSuperiorDepart(type)
      })
    },
    typeFormat(row, col, value, index) {
      return value ? this.departType.find(item => item.value === value).label : ''
    },
    updateDepartment() {
      api.updateDepartment(this.addDepart).then(res => {
        if (res.status === 100) {
          this.dialogFormVisible = false
          this.findDepart(this.limit, this.currentPage)
          this.$message({
            type: 'success',
            message: '修改部门成功'
          })
        } else {
          this.$message({
            type: 'warning',
            message: res.message
          })
        }
      })
    },
    toggleStatus(id, state) {
      this.$confirm("是否删除", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.updateDepartment({ id, state }).then(res => {
            if (res.status === 100) {
              this.findDepart(this.limit, this.currentPage)
              this.$message({
                type: 'success',
                message: '删除成功'
              })
            } else {
              this.$message({
                type: 'warning',
                message: res.message
              })
            }
          })
        })
        .catch(() => { });
    },
    parseTime(row, col, value, index) {
      return new Date(value).toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: '2-digit' }) + ' ' + new Date(value).toLocaleTimeString('zh-CN', { hour12: false })
    },
    closedDialog() {
      this.addDepart = {
        type: '',
        name: '',
        fatherid: ''
      }
    }
  },
  mounted() {
    this.findDepart(this.limit, this.currentPage)
  }
};
</script>

<style lang="less" scoped>
@import url("../../assets/css/index.css");
.el-header {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  margin-top: 20px;
  margin-bottom: 20px;
  .el-input,
  .el-select,
  .el-date-picker {
    width: 100%;
  }
}
.el-main {
  width: 98%;
  background: #fff;
  margin: 0 auto;
  padding: 0px;
  .contant_head {
    border-left: 3px solid #f7585d;
    background: #f9f9f9;
    height: 40px;
    span {
      line-height: 40px;
      font-size: 20px;
      padding-left: 10px;
      sub {
        font-size: 12px;
      }
    }
  }
}
</style>
<style>
.cell {
  text-align: center;
}
</style>
<style lang="stylus" module="$">
.role_setting_panel {
  :global {
    .el-form-item {
      display: block;
    }
  }
}

.role_warpper {
  :global {
    .el-dialog__header {
      background: rgba(242, 242, 242, 1);
      padding-bottom: 16px;
    }

    .el-dialog__title {
      font-weight: bold;
    }

    .el-form-item__label {
      float: none;
      display: inline-block;
      min-width: 120px;
    }

    .el-checkbox__input.is-checked .el-checkbox__inner, .el-checkbox__input.is-indeterminate .el-checkbox__inner {
      background-color: #F7585D;
      border-color: #F7585D;
    }

    .el-checkbox__input.is-checked+.el-checkbox__label {
      color: #F7585D;
    }

    .el-button--primary {
      color: #fff;
      background-color: #F7585D;
      border-color: #F7585D;
    }
  }
}
</style>




